package thread2;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class Test2 {
    public static void main(String[] args) {
        BlockingQueue<Integer> blockingQueue = new LinkedBlockingQueue<>();
        Thread producer = new Thread(() -> {
            int n = 1;
            while (true) {
                try {
                    blockingQueue.put(n);
                    System.out.println("生产者生产了 " + n);
                    Thread.sleep(1000);
                    n++;
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        producer.start();
        Thread customer = new Thread(() -> {
            while (true) {
                try {
                    int n = blockingQueue.take();
                    System.out.println("消费者消费了 " + n);
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        customer.start();
    }
}
